package com.hedl.practice.diyJava.gather.study_map.tmp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Students {
    List<Student> list;
    Map<String,Integer> cache;

    public Students(List<Student> list){
        this.list = list;
        cache = new HashMap<>();
    }

    /**
     * 根据name查找score，找到返回score，未找到返回-1
     */
    public int getScore(String name){
        // 先在Map中查找:
        Integer score = this.cache.get(name);
        if (score == null){
            score = findInList(name);
            if (score != null){
                this.cache.put(name,score);
            }
        }
        return score == null ? -1 : score.intValue();
    }

    public Integer findInList(String name){
        for (Student ss:this.list){
            if (ss.getName().equals(name)){
                return ss.getScore();
            }
        }
        return null;
    }
}
